#include "CESInterpreter.h"

CESInterpreter::CESInterpreter(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
{
	ui.setupUi(this);
	connectSignals();
}

CESInterpreter::~CESInterpreter()
{
}

void CESInterpreter::connectSignals()
{
    /* File Actions */
    connect(ui.actionNew, SIGNAL(triggered()), this, SLOT(newf()));
	connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(open()));
	connect(ui.actionSave, SIGNAL(triggered()), this, SLOT(save()));
	connect(ui.actionSaveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
	connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(exit()));

    /*  Edit Actions */
    connect(ui.actionUndo, SIGNAL(triggered()), this, SLOT(undo()));
    connect(ui.actionRedo, SIGNAL(triggered()), this, SLOT(redo()));
    connect(ui.actionCut, SIGNAL(triggered()), this, SLOT(cut()));
    connect(ui.actionCopy, SIGNAL(triggered()), this, SLOT(copy()));
    connect(ui.actionPaste, SIGNAL(triggered()), this, SLOT(paste()));
    connect(ui.actionDelete, SIGNAL(triggered()), this, SLOT(del()));
    connect(ui.actionSelectAll, SIGNAL(triggered()), this, SLOT(selectAll()));

    /* Symbols Actions */
    connect(ui.menuSymbols, SIGNAL(actionTriggered(QAction*)), this, SLOT(insertSymbol(QAction*)));
    connect(ui.mainToolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(insertSymbol(QAction*)));

    /* Help Actions */
    connect(ui.actionAbout, SIGNAL(triggered()), this, SLOT(about()));
    
    /* Console Actions */
	connect(ui.console, SIGNAL(onConfirm()), this, SLOT(fetchCode()));
}

void CESInterpreter::insertSymbol(QAction* action)
{
    if (action == ui.actionInsert_AND)
    {
        ui.codeEditor->insertPlainText("and");
    }
    else if (action == ui.actionInsert_OR)
    {
        ui.codeEditor->insertPlainText("or");
    }
    else if (action == ui.actionInsert_CROSS)
    {
        ui.codeEditor->insertPlainText("x");
    }
    else if (action == ui.actionInsert_ELEMENT_OF)
    {
        ui.codeEditor->insertPlainText("elem");
    }
    else if (action == ui.actionInsert_ARROW)
    {
        ui.codeEditor->insertPlainText("->");
    }
    else if (action == ui.actionInsert_IDENTICAL)
    {
        ui.codeEditor->insertPlainText("==");
    }
    else if (action == ui.actionInsert_INTEGERS)
    {
        ui.codeEditor->insertPlainText("Z");
    }
    else if (action == ui.actionInsert_LAMBDA)
    {
        ui.codeEditor->insertPlainText("LAMBDA");
    }
    else if (action == ui.actionInsert_LESS_OR_EQUAL)
    {
        ui.codeEditor->insertPlainText("<=");
    }
    else if (action == ui.actionInsert_NATURAL_NUMBERS)
    {
        ui.codeEditor->insertPlainText(QString::fromUtf8("N"));
    }
    else if (action == ui.actionInsert_NOT)
    {
        ui.codeEditor->insertPlainText("!");
    }
}

void CESInterpreter::fetchCode()
{
	ui.console->interpret(ui.codeEditor->toPlainText());
}

void CESInterpreter::newf()
{
    if (fileName != 0)
    {
        fileName.clear();
    }
    ui.codeEditor->clear();
    ui.tabWidget->setTabText(0, "New file");
}

void CESInterpreter::open()
{
    QString newFileName;
	newFileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/", 
		tr("CE-S file (*.ces);;All types (*.*)"));

    if (!newFileName.isEmpty())
    {
        fileName = newFileName;
        load();
    }
}

void CESInterpreter::load()
{
    if (!fileName.isEmpty())
    {
	    QFile file(fileName);
	    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
	    {
		    QString content;
		    content = file.readAll();
		    file.close();
            ui.codeEditor->setPlainText(content);
            ui.tabWidget->setTabText(0, file.fileName());
	    }
        else
        {
            QMessageBox::information(this, tr("Error"), 
                file.errorString());
        }
    }
}

void CESInterpreter::saveAs()
{
    QString newFileName;
    newFileName = QFileDialog::getSaveFileName(this, tr("Save File As"), "/", 
		tr("CE-S file (*.ces);;All types (*.*)"));

    if (!newFileName.isEmpty())
    {
        fileName = newFileName;
        save();
    }
}

void CESInterpreter::save()
{
    if (fileName == 0 || fileName.isEmpty()) 
    {
        saveAs();
    } 
    else
    {
        QFile file(fileName);
	    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
	    {
            QTextStream stream(&file);
            stream << ui.codeEditor->toPlainText();
            ui.tabWidget->setTabText(0, file.fileName());
		    file.close();
	    }
        else
        {
            QMessageBox::information(this, tr("Error"), 
                file.errorString());
        }
    }
}

void CESInterpreter::exit()
{
    // TODO Exit Dialog for unsaved progress
	QApplication::exit();
}

void CESInterpreter::about()
{
    QDialog* d;
    d = new QDialog(0, Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
    ui_dialog.setupUi(d);
    d->setFixedSize(458, 338);
    d->show();
}

void CESInterpreter::undo()
{
    ui.codeEditor->undo();
}

void CESInterpreter::redo()
{
    ui.codeEditor->redo();
}

void CESInterpreter::cut()
{
    ui.codeEditor->cut();
}

void CESInterpreter::copy()
{
    ui.codeEditor->copy();
}

void CESInterpreter::paste()
{
    ui.codeEditor->paste();
}

void CESInterpreter::del()
{
	// TODO
}

void CESInterpreter::selectAll()
{
    ui.codeEditor->selectAll();
}
